package a.b;

import java.util.HashMap;
import android.database.Cursor;
import android.database.CursorWrapper;
import android.text.TextUtils;
import android.util.Log;

public class SectionCursor extends CursorWrapper {
	private static final String TAG = "SectionCursor";

	private HashMap<Integer, Integer> mIndexMap = new HashMap<Integer, Integer>();
	private HashMap<Integer, String> mSectionMap = new HashMap<Integer, String>();
	private boolean mSectionRow;
	private int mSectionIndex;
	private int mPosition;

	/**
	 * 
	 * @param cursor
	 * @param stringColumnName
	 */
	public SectionCursor(Cursor cursor, String stringColumnName) {
		super(cursor);
		Log.d(TAG, "create section cursor:start, count=" + cursor.getCount());
		mSectionIndex = cursor.getColumnIndexOrThrow(stringColumnName);
		String section = null;
		String firstLetter = null;
		String prevLetter = null;
		int position = 0;
		cursor.moveToPosition(-1);
		while (cursor.moveToNext()) {

			// section
			section = cursor.getString(mSectionIndex);
			if (TextUtils.isEmpty(section)) {
				section = "etc";
			}

			// firstLetter
			firstLetter = String.valueOf(section.charAt(0));
			if (!firstLetter.equalsIgnoreCase(prevLetter)) {
				// register section
				mSectionMap.put(position++, firstLetter);
			}

			// register swap position
			mIndexMap.put(position++, cursor.getPosition());

			prevLetter = firstLetter;
		}
		Log.d(TAG, "create section cursor:end, count=" + getCount());
	}

	@Override
	public int getCount() {
		// cursor count + section count
		return super.getCount() + mSectionMap.size();
	}

	@Override
	public int getPosition() {
		return mPosition;
	}

	@Override
	public boolean moveToPosition(int position) {
		mPosition = position;
		if (mIndexMap.containsKey(mPosition)) {
			mSectionRow = false;
			return super.moveToPosition(mIndexMap.get(mPosition));
		}
		mSectionRow = true;
		return true;
	}

	@Override
	public String getString(int columnIndex) {
		if (mSectionRow) {
			if (mSectionIndex == columnIndex) {
				return mSectionMap.get(mPosition);
			} else {
				return null;
			}
		}
		return super.getString(columnIndex);
	}

	@Override
	public long getLong(int columnIndex) {
		if (mSectionRow) {
			return -1;
		}
		return super.getLong(columnIndex);
	}

	@Override
	public double getDouble(int columnIndex) {
		if (mSectionRow) {
			return Double.MIN_VALUE;
		}
		return super.getDouble(columnIndex);
	}

	@Override
	public float getFloat(int columnIndex) {
		if (mSectionRow) {
			return Float.MIN_VALUE;
		}
		return super.getFloat(columnIndex);
	}

	@Override
	public int getInt(int columnIndex) {
		if (mSectionRow) {
			return Integer.MIN_VALUE;
		}
		return super.getInt(columnIndex);
	}

	@Override
	public short getShort(int columnIndex) {
		if (mSectionRow) {
			return Short.MIN_VALUE;
		}
		return super.getShort(columnIndex);
	}

	@Override
	public byte[] getBlob(int columnIndex) {
		if (mSectionRow) {
			return null;
		}
		return super.getBlob(columnIndex);
	}
}
